package com.leaf.lowcode.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.API.DoorFunctionApi;
import com.leaf.lowcode.API.DoorGateApi;
import com.leaf.lowcode.entity.Doorlist;
import com.leaf.lowcode.entity.Doorstate;
import com.leaf.lowcode.entity.door_control.EventsRequest;
import com.leaf.lowcode.entity.door_control.GetDoorStateRequest;
import com.leaf.lowcode.mapper.DoorlistMapper;
import com.leaf.lowcode.utils.RegularInstance;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * 获取门禁点事件和状态
 */
@Component
public class SendDoorInAndOut {
    @Autowired
    private DoorlistMapper doorlistMapper;

    //@Scheduled(initialDelay = 20*100,fixedRate = 15*100)
    @Scheduled(fixedRate = 200 * 100)
    void execute() {

        System.out.println("获取门禁点事件和状态");
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
        DoorGateApi doorGateApi = new DoorGateApi();
        GetDoorStateRequest getDoorStateRequest = null;
        DoorFunctionApi doorFunctionApi = new DoorFunctionApi();
        EventsRequest eventsRequest = null;
        List<Doorlist> doorlists = doorlistMapper.selectList(null);
        Iterator<Doorlist> iterator = doorlists.iterator();

        //获取门禁点列表
        while (iterator.hasNext()) {
            Doorstate doorstate = new Doorstate();
            getDoorStateRequest = new GetDoorStateRequest();
            eventsRequest = new EventsRequest();
            ArrayList<String> doorindex = new ArrayList<String>();


            Doorlist doorlist = iterator.next();
            if (doorlist.getDoorName().contains("F") == true) {
                System.out.println("进入id");
                System.out.println(doorlist.getDoorIndexCode() + doorlist.getDoorName());
                doorindex.add(doorlist.getDoorIndexCode());
                getDoorStateRequest.setDoorIndexCodes(doorindex);
                eventsRequest.setDoorIndexCodes(doorindex);
                eventsRequest.setPageNo(1);
                eventsRequest.setPageSize(10);


                /**
                 * 门禁事件
                 */
                String even = doorFunctionApi.events(eventsRequest);
                System.out.println("iterator.next();" + iterator.next().getDoorIndexCode());


                JSONObject jsonlist = JSONObject.parseObject(even);
                JSONArray doorEvenList = (JSONArray) ((JSONObject) jsonlist.get("data")).get("list");
                System.out.println(doorEvenList);

                if (subDeviceDTOList != null) {
                    for (SubDeviceDTO dto : subDeviceDTOList) {
                        String pid = dto.getPid();
                        String sn = dto.getSn();
                        String din = dto.getDin();
                        if (doorlist.getDoorIndexCode().equals(sn)) {
                            JSONObject standardData = new JSONObject();
                            standardData.put("reportTs", System.currentTimeMillis());
                            JSONObject profile = new JSONObject();
                            profile.put("modelId", 200640);
                            profile.put("poiCode", "w0703003");
                            profile.put("appType","access_control");
                            standardData.put("profile", profile);
                            WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                            JSONObject events = new JSONObject();
                            JSONObject event = new JSONObject();
                            event.put("eventType", 1);
                            event.put("eventTs", System.currentTimeMillis());
                            event.put("describe", "");
                            event.put("doorEvenList", doorEvenList);
                            events.put("in",event);
                            standardData.put("events", events);
                            standardData.put("properties", new JSONObject());
                            try {

                                System.out.println("上传数据");
                                String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                                System.out.println("发送设备运行时事件数据==" + devRunEvent);
                                System.out.println("--------------------------------开始传输设备运行时事件数据");
                                wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);
                            } catch (Exception e) {
                                System.out.println("设备运行时事件数据传输出现异常");
                            }

                        }

                    }

                }
            }

        }
    }
}
